<?php
namespace  Backend\Model;
use Think\Model;

class  ApplyModel extends  Model
{
    // 定义表单验证的规则
    protected $_validate = array(
        //array('media_id', 'require', '必须要选择一个主分类！', 1),
        array('apply_name', 'require', '分组名称不能为空！', 1),
        array('apply_name', '', '应用名称已存在！', 0, 'unique', 3),

    );

    public function get_apply($pageSize = 10) {

        $where = array('a.user_id' =>$_SESSION['id']);
        $count = $this->alias('a')->where($where)->count();
        $page = new \Think\Page($count, $pageSize);
        // 配置翻页的样式
        $page->setConfig('prev', '上一页');
        $page->setConfig('next', '下一页');
        $data['page'] = $page->show();
        /************************************** 取数据 ******************************************/

        $data['data'] = $this->alias('a')
            ->field('a.id,a.access_mode,a.found_time,a.apply_status,a.apply_name,a.apply_platform,a.apply_classify,a.apply_synopsis,sum(b.union_view) as union_view ,sum(b.union_click) as union_click,sum(b.union_click_rate) as union_click_rate ,sum(b.ad_returns) as ad_returns,sum(b.ad_requests) as ad_requests,sum(b.code_price) as sum')
            ->join('dsp_price as b  on b.apply_id = a.id','LEFT')
            ->where($where)
            ->group('a.id')
            ->order('a.id desc')
            ->limit($page->firstRow.','.$page->listRows)
            ->select();
       // echo $this->getLastSql();
        return $data;
    }




    // 添加前
    protected function _before_insert(&$data) {
        $data['found_time']=time();
        $data['user_id'] =$_SESSION['id'];
    }

    // 修改前
    protected function _before_update(&$data) {
        $data['found_time']=time();
    }
}